<?php

namespace App\Admin\Controllers;

use App\Models\CompanyProfile;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form as  WidgetsForm;

class CompanyProfileController extends Controller
{
    use ModelForm;

    public function introduce($language = 'cn')
    {
        return Admin::content(function (Content $content) use($language) {
            $content->header('公司简介');

            $this->showFormParameters($content);
            $intriduce = CompanyProfile::where('language', $language)
                ->first(['id', 'name', 'summary', 'address', 'telephone', 'fax', 'email', 'petrel_image',
                    'province_image', 'petrel_publishers', 'province_publishers', 'connection', 'explain',
                    'node_intro']);

            $images = ['petrel_image' => asset($intriduce->petrel_image), 'province_image' => asset($intriduce->province_image)];
            $form = new WidgetsForm($images);
            $form->method('post');

            if ($intriduce)
            {
                $form->hidden('id')->default($intriduce->id);
                $form->text('name', '公司名称')->default($intriduce->name)->rules('required');
                if ($language == 'en')
                {
                    $form->editor('summary', '公司简介')->default($intriduce->summary)->rules('required');
                    $form->text('address', '地址')->default($intriduce->address)->rules('required');
                    $form->text('telephone', '联系电话')->default($intriduce->telephone)->rules('required');
                    $form->text('fax', '传真')->default($intriduce->fax)->rules('required');
                    $form->email('email', '邮箱')->default($intriduce->email)->rules('required');
                } else {
                    $form->image('petrel_image', '广东海燕电子音像出版社介绍图')
                        ->uniqueName()
                        ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                        ->rules('required|max:2048', ['max' => '图片超过2Mb']);

                    $form->editor('petrel_publishers', '广东海燕电子音像出版社')
                        ->default($intriduce->petrel_publishers)->rules('required');

                    $form->image('province_image', '广东语言音像电子出版社介绍图')
                        ->uniqueName()
                        ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                        ->rules('required|max:2048', ['max' => '图片超过2Mb']);


                    $form->editor('province_publishers', '广东语言音像电子出版社')
                        ->default($intriduce->province_publishers)->rules('required');
                    $form->editor('connection', '联系我们')->default($intriduce->connection)->rules('required');
                    $form->editor('explain', '投稿须知')->default($intriduce->explain)->rules('required');
                }

                $form->textarea('node_intro', '网站简介')->default($intriduce->node_intro)->rules('required');

            } else {
                $form->text('name', '公司名称')->rules('required');
                if ($language == 'en')
                {
                    $form->editor('summary', '公司简介')->rules('required');
                    $form->text('address', '地址')->rules('required');
                    $form->text('telephone', '联系电话')->rules('required');
                    $form->text('fax', '传真')->rules('required');
                    $form->email('email', '邮箱')->rules('required');;
                } else {
                    $form->image('petrel_image', '广东海燕电子音像出版社介绍图')->uniqueName();
                    $form->editor('petrel_publishers', '广东海燕电子音像出版社')->rules('required');
                    $form->image('province_image', '广东语言音像电子出版社介绍图')->uniqueName();
                    $form->editor('province_publishers', '广东语言音像电子出版社')->rules('required');
                    $form->textarea('connection', '联系我们')->rules('required');
                    $form->editor('explain', '投稿须知')->rules('required');
                }
                $form->textarea('node_intro', '网站简介')->rules('required');
            }
            $form->hidden('language')->default($language);
            $form->action(admin_base_path('subIntroduce'));

            $title = '中文介绍';
            if ($language == 'en')
                $title = '英文介绍';

            $content->body(new Box($title, $form));
        });
    }


    public function postIntroduce()
    {
        $parameters = request()->except(['_pjax', '_token']);
        if (isset($parameters['id']))
        {
            $introduce = CompanyProfile::find($parameters['id']);
        } else {
            $introduce = new CompanyProfile();
        }

        $introduce->name = isset($parameters['name']) ? $parameters['name'] : '';
        $introduce->summary = isset($parameters['summary']) ? $parameters['summary'] : '';
        $introduce->address = isset($parameters['address']) ? $parameters['address'] : '';
        $introduce->telephone = isset($parameters['telephone']) ? $parameters['telephone'] : '';
        $introduce->fax = isset($parameters['fax']) ? $parameters['fax'] : '';
        $introduce->email = isset($parameters['email']) ? $parameters['email'] : '';
        $introduce->language = isset($parameters['language']) ? $parameters['language'] : '';

        if (isset($parameters['petrel_image'])) {
            $petrelImage = $this->upload($parameters['petrel_image']);
            $introduce->petrel_image = $petrelImage;
        }
        $introduce->petrel_publishers = isset($parameters['petrel_publishers']) ? $parameters['petrel_publishers'] : '';

        if (isset($parameters['province_image'])) {
            $provinceImage = $this->upload($parameters['province_image']);
            $introduce->province_image = $provinceImage;
        }
        $introduce->province_publishers = isset($parameters['province_publishers']) ? $parameters['province_publishers'] : '';

        $introduce->connection = isset($parameters['connection']) ? $parameters['connection'] : '';
        $introduce->explain = isset($parameters['explain']) ? $parameters['explain'] : '';
        $introduce->node_intro = isset($parameters['node_intro']) ? $parameters['node_intro'] : '';

        $introduce->save();

        admin_toastr('更新成功', 'success');

        return redirect(admin_base_path('companyIntroduce/' . $parameters['language']));
    }


    /**
     *
     * @param unknown $content
     */
    protected function showFormParameters($content)
    {
        $parameters = request()->except(['_pjax', '_token']);

        if (!empty($parameters)) {

            ob_start();

            dump($parameters);

            $contents = ob_get_contents();

            ob_end_clean();

            $content->row(new Box('Form parameters', $contents));
        }
    }

    public function upload($image, $key = null)
    {
        $file = data_get($image, $key);
        if (!$file) return '';

//        $originName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();

        $fileName = md5(time() . rand(0, 10000)) . '.' . $extension;

        $savePath = public_path('uploads/images');

        if (!is_dir($savePath))
            mkdir($savePath, 0777);

        $file->move($savePath, $fileName);
        $path = '/uploads/images/' . $fileName;

        return $path;
    }
}
